package com.mirei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

/**
 * @author mirei
 */
@RestController
public class UserController {
    @GetMapping("/user")
    public String userInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println("name: " + name);
        System.out.println("authorities = " + authorities);
        return name + " " + authorities;
    }

    @GetMapping("/userInfo")
    public void getUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println("name: " + name);
        System.out.println("authorities = " + authorities);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                if (null == authentication){
                    System.out.println("authentication is null");
                    return;
                }
                String name = authentication.getName();
                Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
                System.out.println("name: " + name);
                System.out.println("authorities = " + authorities);
            }
        }).start();
    }

    @GetMapping("/userAsync")
    public void userInfo(HttpServletRequest request, HttpServletResponse response){
        AsyncContext asyncContext = request.startAsync();
        CompletableFuture.runAsync(()->{
            try{
                PrintWriter out = asyncContext.getResponse().getWriter();
                out.write("hello javaBoy!");
                asyncContext.complete();
            }catch (IOException e){
                e.printStackTrace();
            }
        });
    }

    @RequestMapping("/authentication")
    public void authentication(Authentication authentication){
        System.out.println("authentication = " + authentication);
    }

    @RequestMapping("/principal")
    public void principal(Principal principal){
        System.out.println("principal = " + principal);
    }
}
